﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;

namespace mat2tab_fun.functionPlots
{
    class PolynomialFunctionPlot : FunctionPlot
    {
        // wielomian 1 stopnia
        public PolynomialFunctionPlot(bool testMonotonicznosci, Grid plotGrid, int size, double a1, double a2)
            : base(testMonotonicznosci, plotGrid, size)
        {
            // int iloscLinii = (int) srodek / size; // ilosc linii na jednej polowie
            Line l1 = new Line();
            l1.X1 = -halfX;                   l1.X1 += halfX;
            l1.Y1 = a1 * -halfY + a2 * size;  l1.Y1 = l1.Y1 * (-1) + halfY;
            l1.X2 = halfX;                    l1.X2 += halfX;
            l1.Y2 = a1 * halfY + a2 * size;   l1.Y2 = halfY - l1.Y2;
            if (testMonotonicznosci)
            {
                if (l1.Y1 > l1.Y2)
                    l1.Stroke = Brushes.Red;
                else
                    l1.Stroke = Brushes.Green;
            }
            else
                l1.Stroke = Brushes.Blue;
            lines.Add(l1);
            DrawPlot();
        }

        // wielomian 2 stopnia
        public PolynomialFunctionPlot(bool testMonotonicznosci, Grid plotGrid, int size, double a1, double a2, double a3)
            : base(testMonotonicznosci, plotGrid, size)
        {
            for (double x = -halfX / size; x <= halfX / size; )
            {
                Line l = new Line();
                l.X1 = x * size + halfX;
                l.Y1 = a1 * x * x * size + a2 * x * size + a3 * size;
                if (l.Y1 >= 0)
                    l.Y1 = halfY - l.Y1;
                else
                    l.Y1 = l.Y1 * (-1) + halfY;

                x += 0.1;

                l.X2 = x * size + halfX;
                l.Y2 = a1 * x * x * size + a2 * x * size + a3 * size;
                if (l.Y2 >= 0)
                    l.Y2 = halfY - l.Y2;
                else
                    l.Y2 = l.Y2 * (-1) + halfY;
                if (testMonotonicznosci)
                {
                    if (l.Y1 > l.Y2)
                        l.Stroke = Brushes.Red;
                    else
                        l.Stroke = Brushes.Green;
                }
                else
                    l.Stroke = Brushes.Blue;
                lines.Add(l);
            }
            DrawPlot();
        }

        // wielomian 3 stopnia
        public PolynomialFunctionPlot(bool testMonotonicznosci, Grid plotGrid, int size, double a1, double a2, double a3, double a4)
            : base(testMonotonicznosci, plotGrid, size)
        {
            for (double x = -halfX / size; x <= halfX / size; )
            {
                Line l = new Line();
                l.X1 = x * size + halfX;
                l.Y1 = a1 * Math.Pow(x,3) * size + a2 * Math.Pow(x,2) * size + a3 * x * size + a4 * size;
                if (l.Y1 >= 0)
                    l.Y1 = halfY - l.Y1;
                else
                    l.Y1 = l.Y1 * (-1) + halfY;
                
                x += 0.1;

                l.X2 = x * size + halfX;
                l.Y2 = a1 * Math.Pow(x, 3) * size + a2 * Math.Pow(x, 2) * size + a3 * x * size + a4 * size;
                if (l.Y2 >= 0)
                    l.Y2 = halfY - l.Y2;
                else
                    l.Y2 = l.Y2 * (-1) + halfY;
                if (testMonotonicznosci)
                {
                    if (l.Y1 > l.Y2)
                        l.Stroke = Brushes.Red;
                    else
                        l.Stroke = Brushes.Green;
                }
                else
                    l.Stroke = Brushes.Blue;
                lines.Add(l);
            }
            DrawPlot();
        }

        // wielomian 4 stopnia
        public PolynomialFunctionPlot(bool testMonotonicznosci, Grid plotGrid, int size, double a1, double a2, double a3, double a4, double a5)
            : base(testMonotonicznosci, plotGrid, size)
        {
            for (double x = -halfX / size; x <= halfX / size; )
            {
                Line l = new Line();
                l.X1 = x * size + halfX;
                l.Y1 = a1 * Math.Pow(x, 4) * size + a2 * Math.Pow(x, 3) * size + a3 * Math.Pow(x, 2) * size + a4 * x * size + a5 * size;
                if (l.Y1 >= 0)
                    l.Y1 = halfY - l.Y1;
                else
                    l.Y1 = l.Y1 * (-1) + halfY;

                x += 0.1;

                l.X2 = x * size + halfX;
                l.Y2 = a1 * Math.Pow(x, 4) * size + a2 * Math.Pow(x, 3) * size + a3 * Math.Pow(x, 2) * size + a4 * x * size + a5 * size;
                if (l.Y2 >= 0)
                    l.Y2 = halfY - l.Y2;
                else
                    l.Y2 = l.Y2 * (-1) + halfY;
                if (testMonotonicznosci)
                {
                    if (l.Y1 > l.Y2)
                        l.Stroke = Brushes.Red;
                    else
                        l.Stroke = Brushes.Green;
                }
                else
                    l.Stroke = Brushes.Blue;
                lines.Add(l);
            }
            DrawPlot();
        }

        // wielomian 5 stopnia
        public PolynomialFunctionPlot(bool testMonotonicznosci, Grid plotGrid, int size, double a1, double a2, double a3, double a4, double a5, double a6)
            : base(testMonotonicznosci, plotGrid, size)
        {
            for (double x = -halfX / size; x <= halfX / size; )
            {
                Line l = new Line();
                l.X1 = x * size + halfX;
                l.Y1 = a1 * Math.Pow(x, 5) * size + a2 * Math.Pow(x, 4) * size + a3 * Math.Pow(x, 3) * size + a4 * Math.Pow(x, 2) * size + a5 * x * size + a6 * size;
                if (l.Y1 >= 0)
                    l.Y1 = halfY - l.Y1;
                else
                    l.Y1 = l.Y1 * (-1) + halfY;

                x += 0.1;

                l.X2 = x * size + halfX;
                l.Y2 = a1 * Math.Pow(x, 5) * size + a2 * Math.Pow(x, 4) * size + a3 * Math.Pow(x, 3) * size + a4 * Math.Pow(x, 2) * size + a5 * x * size + a6 * size;
                if (l.Y2 >= 0)
                    l.Y2 = halfY - l.Y2;
                else
                    l.Y2 = l.Y2 * (-1) + halfY;
                if (testMonotonicznosci)
                {
                    if (l.Y1 > l.Y2)
                        l.Stroke = Brushes.Red;
                    else
                        l.Stroke = Brushes.Green;
                }
                else
                    l.Stroke = Brushes.Blue;
                lines.Add(l);
            }
            DrawPlot();
        }

    }
}
